<?php
if (!defined('ROOT')) exit('SYTbHelper');
//封禁相关
class reply_model extends base {
	private $replylist;
	private $tmpreplylist;
	static private $_instance=NULL;
	static public function getInstance () {
		if (!self::$_instance) self::$_instance=new self;
		return self::$_instance;
	}
	public function __construct () {
		parent::init();
		$_ENV['_now']='reply';
		$this->replylist=$this->db('get','replylist');
		$this->reply_inittmplist();
	}
	private function reply_inittmplist ($renew=FALSE) { //建立临时表
		if ($renew) { //强制更新
			$this->tmpreplylist=$this->reply_getTmplistFromList();
			$this->db('set','tmpreplylist',$this->tmpreplylist);
			$this->setval('lastreply',date('Ymd'));
		} else {
			$last=$this->getval('lastreply');
			if ($last!=date('Ymd')) {
				$this->tmpreplylist=$this->reply_getTmplistFromList();
				$this->db('set','tmpreplylist',$this->tmpbanlist);
				$this->setval('lastreply',date('Ymd'));
			} else {
				$this->tmpreplylist=$this->db('get','tmpreplylist');
			}
		}
	}
	private function reply_getTmplistFromList () { //从原始数据建立临时数据
		$num=intval($this->getval('reply_num'));
		//$num=2;
		if ($num<1) return array();
		$r=array();
		$arr=$this->replylist;
		$replynum=count($this->replylist);
		for ($i=1;$i<=$num;$i++) {
			$start=($replynum-1)*$i;
			$keys=range($start,$start+$replynum-1);
			$arr=array_combine($keys,array_values($arr));
			$r=array_merge($r,$arr);
		}
		return $r;
	}
	private function reply_gettmpnum () {
		return count($this->tmpreplylist);
	}
	private function reply_getonetmp () {
		return each($this->tmpreplylist);
	}
	private function reply_removetmp ($id) {
		unset($this->tmpreplylist[$id]);
		$this->db('set','tmpreplylist',$this->tmpreplylist);
	}
	private function reply_add ($data) { //$id,$day,$fid,$tieba
		$this->replylist[]=$data;
		$this->db('set','replylist',$this->replylist);
	}
	private function reply_remove ($id) {
		unset($this->replylist[$id]);
		$this->db('set','replylist',$this->replylist);
	}
	private function client_post ($id,$tieba,$fid,$tid,$content='') {
		if (empty($content)) {
			switch (mt_rand(0,2)) {
				case 0:
					$content="我又机智地水了一贴——".date('Y年m月d日H时i分s秒');
					break;
				case 1:
					$content="于".date('Y年m月d日H时i分s秒')."调戏度受";
					break;
				case 2:
					$content="我是来水经验的——".date('Y年m月d日H时i分s秒');
					break;
			}
		} else {
			$form=array('{$time}','{$Y}','{$m}','{$d}','{$H}','{$i}','{$s}');
			$to=array(time(),date('Y'),date('m'),date('d'),date('H'),date('i'),date('s'));
			$content=str_replace($form,$to,$content);
		}
		$formdata=array(
			'anonymous'=>0,
			'content'=>$content,
			'fid'=>$fid,
			'kw'=>$tieba,
			'tid'=>$tid,
			'title'=>''
		);
		$formdata=client_mkInfo(array('anonymous'=>0,'content'=>$content,'fid'=>$fid,'kw'=>$tieba),array('tid'=>$tid,'title'=>''));
		$sign_json=curl_get($this->mkurl('c/c/post/add','c'),array('post'=>$formdata));
		$re=json_decode($sign_json,1);
		if (!$re) {
			$out.='<li class="list-group-item">'.$tieba.'吧：JSON解析错误</li>';
			return FALSE;
		}
		if ($re['error_code']==0) {
			echo '<li class="list-group-item list-group-item-success"><strong>',$tieba,'吧</strong>：使用 Windows8 客户端发帖成功，<a href="http://tieba.baidu.com/p/',$tid,'" target="_blank">查看帖子</a></li>';
			$this->reply_removetmp($id);
		} elseif ($re['error_code']==5) {
			echo '<li class="list-group-item list-group-item-warning"><strong>',$tieba,'吧</strong>：需要输入验证码</li>';
			$this->reply_removetmp($id);
		} elseif ($re['error_code']==7) {
			echo '<li class="list-group-item list-group-item-info"><strong>',$tieba,'吧</strong>：操作太频繁</li>';
		} elseif ($re['error_code']==8) {
			echo '<li class="list-group-item list-group-item-danger"><strong>',$tieba,'吧</strong>：ID被封禁</li>';
		} else {
			echo '<li class="list-group-item"><strong>',$tieba,'吧</strong>：未知错误</li>';
		}
	}
	//添加
	public function onadd () {
		if (isset($_POST['submit'])) {
			$id=$_POST['content'];
			$tid=intval($_POST['tid']);
			$tieba=htmlspecialchars($_POST['tieba']);
			if (empty($tid) || empty($tieba)) $this->DisplayError('请填写完整！');
			$fid=$this->get_fid($tieba);
			$data=array('tieba'=>$tieba,'fid'=>$fid,'tid'=>$tid,'content'=>$content);
			$this->reply_add($data);
			$this->ShowTips('添加成功！贴吧fid为：'.$fid.'，贴子ID为'.$tid,'success');
		} else {
			include(TPL.'reply_add.html');
		}
	}
	//移除
	public function ondel () {
		$id=intval($_GET['id']);
		if (!isset($_GET['id']) || $_GET['id']=='') $this->DisplayError('参数错误！');
		$this->reply_remove($id);
		$this->ShowTips('成功删除','success');
	}
	//列出
	public function onlist () {
		$reply=$this->replylist;
		$list='';
		foreach ($reply as $key=>$row) {
			$list.='<tr><td>'.htmlspecialchars($row['tieba']).'</td><td>'.$row['tid'].'</td><td><a href="?m=reply&a=del&id='.$key.'" class="btn btn-danger">删除</a></td></tr>';
		}
		include(TPL.'reply_list.html');
	}
	//进行回复
	public function reply (){
		if (!$this->is_login()) $this->DisplayError('Cookie错误');
		$i=0;
		ob_start();
		while ($one=$this->reply_getonetmp()) {
			if ($i==6) break;
			$i++;
			list($id,$row)=$one;
			$this->client_post($id,$row['tieba'],$row['fid'],$row['tid'],$row['content']);
			sleep(1.5); //休息1.5秒继续
		}
		$num=$this->reply_gettmpnum();
		if ($num!=0) $continue='<div class="alert alert-success"><a href="?m=reply&a=test" class="btn btn-success">还剩下'.$num.'个贴没有回(shui)，点击这里继续</a></div>';
		else $continue='<div class="alert alert-success">已完成回贴任务</div>';
		if (defined('CRON')) {
			ob_end_clean();
			echo 'CRON';
		} else {
			$info=ob_get_clean();
			include(TPL.'reply_result.html');
		}
	}
	//CRON
	public function cron () {
		$this->reply();
		$num=$this->reply_gettmpnum();
		if ($num!=0) {
			task_add('reply');
		}
	}
	//测试
	public function ontest () {
		$this->reply();
	}
}
?>